package com.yunshangwoniu.core.baseService.impl;

import com.yunshangwoniu.core.baseService.PageService;
import com.yunshangwoniu.core.page.Page;
import com.yunshangwoniu.core.page.PageTemplate;
import com.yunshangwoniu.core.page.PageTemplate.PageNoContent;
import com.yunshangwoniu.core.utils.Logger;

public class DefaultPageService<T> implements PageService<T>
{
	private String pageTemplatePath;							//page xml 模板的路径
	private PageTemplate template;
	
	@Override
	public void htmlBuilder(Page<T> page,String templateId) 
	{
		long currentPageNo = page.getCurrentPageNum();
		long pageNum = page.getAllPageNum();
		StringBuilder sb = new StringBuilder("");
		sb.append(String.format(new StringBuilder(template.getPageHeader()).toString(),page.getCountOfPerPage(),page.getAllPageNum(),page.getAllCount()));
		if(pageNum==0)
		{
			page.setPageCententHTML(sb.toString());
			return;
		}
		if(currentPageNo!=1)
		{
			PageNoContent previvousPage = template.getPreviousPage();
			sb.append(String.format(new StringBuilder(previvousPage.getUrlOn()).append(page.getUrl().replaceAll("pageNo="+currentPageNo, "pageNo="+(currentPageNo-1))).append(previvousPage.getUrlOff()).toString(), page.getUrl().replaceAll("pageNo="+currentPageNo, "pageNo="+1)));
		}
		int size = template.getPageContentSize();
		if(size>=pageNum)
		{
			sb.append(getPageContentNo(1,(int)pageNum,(int)currentPageNo,template,page.getUrl()));
		}else 
		{    
			if(currentPageNo<=(size/2+1))
			{
				sb.append(getPageContentNo(1,(int)size,(int)currentPageNo,template,page.getUrl()));
			}else if(pageNum-currentPageNo>=(size-(size/2+1)))
			{
				sb.append(getPageContentNo((int)(currentPageNo-(size/2+1))+1,(int)currentPageNo+(size-(size/2+1)),(int)currentPageNo,template,page.getUrl()));
			}else
			{
				sb.append(getPageContentNo((int)(page.getAllPageNum()-size)+1,(int)page.getAllPageNum(),(int)currentPageNo,template,page.getUrl()));
			}
		}
		if(currentPageNo!=pageNum)
		{
			PageNoContent nextPage = template.getNextPage();
			sb.append(String.format(new StringBuilder(nextPage.getUrlOn()).append(page.getUrl().replaceAll("pageNo="+currentPageNo, "pageNo="+(currentPageNo+1))).append(nextPage.getUrlOff()).toString(), page.getUrl().replaceAll("pageNo="+currentPageNo, "pageNo="+pageNum)));
		}
	Logger.debug(this.getClass(),"html===="+sb.toString());
	page.setPageCententHTML(sb.toString());
	}

	/*protected void getPageTemplate(String pageTemplatePath) throws Exception
	{
		String path = this.getClass().getClassLoader().getResource("").getPath()+pageTemplatePath;
		JAXBContext context = JAXBContext.newInstance(PageTemplate.class);
		FileReader fr = new FileReader(path);
		Unmarshaller um = context.createUnmarshaller();
		template = (PageTemplate) um.unmarshal(fr);
		Logger.debug(this.getClass(),"=========="+template.getPageHeader());
	}*/
	
	
	private String getPageContentNo(int index,int index2,int currentPageNo,PageTemplate template,String url)
	{
		StringBuilder sb = new StringBuilder();
		PageNoContent pageContent = template.getContentPage();
		for(int i = index;i<currentPageNo;i++)
		{
			sb.append(String.format(new StringBuilder().append(pageContent.getUrlOn()).append(url.replaceAll("pageNo="+currentPageNo, "pageNo="+i)).append(pageContent.getUrlOff()).toString(), i+""));
		}
		sb.append(String.format(new StringBuilder().append(template.getCurrentPage().getUrlOn()).append(template.getCurrentPage().getUrlOff()).toString(), currentPageNo+""));
		for(int i = (int) (currentPageNo+1);i<=index2;i++)
		{
			sb.append(String.format(new StringBuilder().append(pageContent.getUrlOn()).append(url.replaceAll("pageNo="+currentPageNo, "pageNo="+i)).append(pageContent.getUrlOff()).toString(), i+""));
		}
		return sb.toString();
	}

	public DefaultPageService() throws Exception
	{
		super();
	}

	public String getPageTemplatePath()
	{
		return pageTemplatePath;
	}

	public void setPageTemplatePath(String pageTemplatePath) throws Exception
	{
		this.pageTemplatePath = pageTemplatePath;
		//getPageTemplate(pageTemplatePath);
	}

	public PageTemplate getTemplate()
	{
		return template;
	}

	public void setTemplate(PageTemplate template)
	{
		this.template = template;
	}
	
}
